﻿using System.Windows.Controls;
using appInterfaces;
using System.Messaging;
using System;
using appDataContracts;
namespace appSupplier.UserControls
{
    /// <summary>
    /// Logique d'interaction pour UCMessages.xaml
    /// </summary>
    public partial class UCMessages : UserControl
    {
        private static MessageQueue queue = null;
        public UCMessages()
        {
            InitializeComponent();

            try
            {

            MessageQueue MQueue = new MessageQueue("a-pc\\private$\\msmqservice/msmqservice.svc");
            MQueue.MessageReadPropertyFilter.SetAll();

            // the target type we have stored in the message body
            ((XmlMessageFormatter)MQueue.Formatter).TargetTypes = new Type[] { typeof(SerializedMessage) };

                // read the message from the queue, but only wait for 5sec
                System.Messaging.Message Msg = MQueue.Receive(new TimeSpan(0, 0, 10));

                // read the order from the message body
                SerializedMessage msg = (SerializedMessage)Msg.Body;

                lblInfos.Content = string.Format("{0} {1} <{2}>", msg.client.CLIENTFIRSTNAME, msg.client.CLIENTLASTNAME, msg.client.CLIENTMAIL);

                lblProductName.Content = Msg.Label.ToString();

                lblContentMessage.Text = msg.message;

                // close the message queue
                MQueue.Close();
            }

            // no message present to read
            catch (MessageQueueException ex)
            {
                //Console.WriteLine("There is no message in the queue at this time.");
            }
        }
    }
}
